package love.yuanmengrobot.qianyugarden.service.impl;/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:zjj
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/7/31 15:27
 */

import love.yuanmengrobot.qianyugarden.dao.*;
import love.yuanmengrobot.qianyugarden.entity.*;
import love.yuanmengrobot.qianyugarden.service.BrowseArticlesService;
import love.yuanmengrobot.qianyugarden.utils.RandomUtils;
import love.yuanmengrobot.qianyugarden.utils.SystemConstant;
import love.yuanmengrobot.qianyugarden.vo.BrowseAritclesInfo;
import love.yuanmengrobot.qianyugarden.vo.BrowseArticlesUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Service
public class BrowseArticlesServiceImpl implements BrowseArticlesService {

    @Autowired
    UserEventsDao userEventsDao;
    @Autowired
    UserDao userDao;
    @Autowired
    YuanwenDao yuanwenDao;
    @Autowired
    BookDao bookDao;
    @Autowired
    YuanwenIncidentDao yuanwenIncidentDao;

    @Override
    public Map<String,Object> loadBrowseArticles(String objectId,String userId) {
        BrowseArticlesUserInfo browseArticlesUserInfo = new BrowseArticlesUserInfo();
        Yuanwen yuanwen = new Yuanwen();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        yuanwen.setObjectid(objectId);
        String userid = yuanwenDao.queryAll(yuanwen).get(0).getUserid();


        Map<String, BigDecimal> yuanwenLikes = yuanwenDao.queryUserLikeByUserId(userid);
        if (yuanwenLikes == null) {
            yuanwenLikes = new HashMap<String, BigDecimal>();
        }


        Map<String, BigDecimal> bookLikes = bookDao.queryUserLikeByUserId(userid);
        if (bookLikes == null) {
            bookLikes = new HashMap<String, BigDecimal>();
        }

        /*点赞数*/
        browseArticlesUserInfo.setLike((yuanwenLikes.get("likes")==null?0:yuanwenLikes.get("likes").intValue()) +(bookLikes.get("likes")==null?0:bookLikes.get("likes").intValue()));
        /*收藏数*/
        browseArticlesUserInfo.setCollect((yuanwenLikes.get("collections")==null?0:yuanwenLikes.get("collections").intValue()) +(bookLikes.get("collections")==null?0:bookLikes.get("collections").intValue()));

        UserEvents fans = new UserEvents();
        fans.setTargetid(userid);
        fans.setEventtype("关注");
        fans.setOperationstatus("成功");
        /*被关注数*/
        browseArticlesUserInfo.setFans(userEventsDao.queryCountByUserEvents(fans));
        User user = userDao.queryById(userid);


        /*等级*/
        if(user.getGrade()!=null){
            for(int i=0;i<SystemConstant.grade.length;i++){
                if(user.getGrade()<SystemConstant.grade[i]){
                    user.setGrade(i);
                    break;
                }
            }
        }else {
            user.setGrade(0);
        }


        browseArticlesUserInfo.setUser(user);

        /*近期发布文章*/
        List<Yuanwen> yuanwens = yuanwenDao.queryCreatHistoryByLimit(userid, 0, 5);


        BrowseAritclesInfo browseAritclesInfo = yuanwenDao.queryBrowseAritclesInfo(objectId);

        for (ThousandLanguage thousandLanguage : browseAritclesInfo.getThousandLanguages()) {
            if("语言".equals(thousandLanguage.getType())){
                browseAritclesInfo.getSpecialColumns().add(thousandLanguage);
            } else if ("框架".equals(thousandLanguage.getType())) {
                browseAritclesInfo.getLabel().add(thousandLanguage);
            }
        }
        if(browseAritclesInfo.getYuanwenIncident()==null){
            browseAritclesInfo.setYuanwenIncident(new YuanwenIncident());
        }


        /*是否关注*/
        stringObjectHashMap.put("isFollow",isFollow(userId,userid));


        stringObjectHashMap.put("isLikeYuanWen", isFollow(userId, userid));
        stringObjectHashMap.put("browseArticlesUserInfo",browseArticlesUserInfo);
        stringObjectHashMap.put("RecentYuanwens", yuanwens);
        stringObjectHashMap.put("browseAritclesInfo",browseAritclesInfo);


        /*添加浏览记录*/
        insertBrowsingRecords(userId,objectId);

        System.out.println(browseAritclesInfo);
        System.out.println(browseArticlesUserInfo);
        return stringObjectHashMap;
    }


    /**
     * 添加浏览事件
     * @param userId
     * @param objectId
     */
    public void insertBrowsingRecords(String userId,String objectId){
        UserEvents userEvents = new UserEvents();
        userEvents.setObjectid(RandomUtils.createIdOX("UE"));
        userEvents.setTargetid(objectId);
        userEvents.setUserid(userId);
        userEvents.setEventtype("浏览历史");
        userEvents.setOperationstatus("成功");
        userEvents.setCreatetime(RandomUtils.getTime());
        userEventsDao.insert(userEvents);
    }



    @Override
    public boolean collects(String userId, String targetId) {
        int i=0;
        int j=0;
        UserEvents userEvent = new UserEvents();
        YuanwenIncident yuanwenIncident = new YuanwenIncident();
        yuanwenIncident.setYuanwenid(targetId);

        userEvent.setUserid(userId);
        userEvent.setTargetid(targetId);
        userEvent.setEventtype("收藏");

        List<UserEvents> userEvents = userEventsDao.queryAll(userEvent);
        userEvent.setCreatetime(RandomUtils.getTime());
        if (userEvents.size() > 0) {
            if("成功".equals(userEvents.get(0).getOperationstatus())){
                userEvent.setOperationstatus("取消");
            }else{
                userEvent.setOperationstatus("成功");
            }
            userEvent.setObjectid(userEvents.get(0).getObjectid());
            i=userEventsDao.update(userEvent);
        }else {
            userEvent.setObjectid(RandomUtils.createIdOX("UE"));
            userEvent.setOperationstatus("成功");
            i=userEventsDao.insert(userEvent);
        }

        List<YuanwenIncident> yuanwenIncidents = yuanwenIncidentDao.queryAll(yuanwenIncident);
        System.out.println(yuanwenIncidents);
        if(yuanwenIncidents.size()>0){
            Integer collects = yuanwenIncidents.get(0).getCollections();
            if("成功".equals(userEvent.getOperationstatus())){
                yuanwenIncident.setCollections(++collects);
            }else {
                yuanwenIncident.setCollections(--collects);
            }
            yuanwenIncident.setObjectid(yuanwenIncidents.get(0).getObjectid());
            j=yuanwenIncidentDao.update(yuanwenIncident);
        }else {
            yuanwenIncident.setObjectid(RandomUtils.createIdOX("YI"));
            yuanwenIncident.setCollections(1);
            yuanwenIncident.setLikes(0);
            yuanwenIncident.setObservations(0);
            j=yuanwenIncidentDao.insert(yuanwenIncident);
        }
        return i==j&&j>0;
    }



    /**
     * 点击关注
     * @param userId
     * @param targetId
     */
    public boolean follow(String userId, String targetId) {
        int i=0;
        UserEvents userEvent = new UserEvents();
        userEvent.setUserid(userId);
        userEvent.setTargetid(targetId);
        userEvent.setEventtype("关注");
        List<UserEvents> userEvents = userEventsDao.queryAll(userEvent);
        userEvent.setCreatetime(RandomUtils.getTime());
        if (userEvents.size() > 0) {
            if("成功".equals(userEvents.get(0).getOperationstatus())){
                userEvent.setOperationstatus("取消");
            }else{
                userEvent.setOperationstatus("成功");
            }
            userEvent.setObjectid(userEvents.get(0).getObjectid());
            i=userEventsDao.update(userEvent);
        }else {
            userEvent.setObjectid(RandomUtils.createIdOX("UE"));
            userEvent.setOperationstatus("成功");
            i=userEventsDao.insert(userEvent);
        }
        return i>0;
    }

    /**
     * 是否关注目标用户
     * @param userId
     * @param targetId
     * @return
     */
    public boolean isFollow(String userId, String targetId) {
        UserEvents userEvent = new UserEvents();
        userEvent.setUserid(userId);
        userEvent.setTargetid(targetId);
        userEvent.setEventtype("关注");
        userEvent.setOperationstatus("成功");
        List<UserEvents> userEvents = userEventsDao.queryAll(userEvent);
        return userEvents.size()>0;
    }

    @Override
    public boolean like(String userId, String targetId) {
        int i=0;
        int j=0;
        UserEvents userEvent = new UserEvents();
        YuanwenIncident yuanwenIncident = new YuanwenIncident();
        yuanwenIncident.setYuanwenid(targetId);

        userEvent.setUserid(userId);
        userEvent.setTargetid(targetId);
        userEvent.setEventtype("点赞");

        List<UserEvents> userEvents = userEventsDao.queryAll(userEvent);
        userEvent.setCreatetime(RandomUtils.getTime());
        if (userEvents.size() > 0) {
            if("成功".equals(userEvents.get(0).getOperationstatus())){
                userEvent.setOperationstatus("取消");
            }else{
                userEvent.setOperationstatus("成功");
            }
            userEvent.setObjectid(userEvents.get(0).getObjectid());
            i=userEventsDao.update(userEvent);
        }else {
            userEvent.setObjectid(RandomUtils.createIdOX("UE"));
            userEvent.setOperationstatus("成功");
            i=userEventsDao.insert(userEvent);
        }

        List<YuanwenIncident> yuanwenIncidents = yuanwenIncidentDao.queryAll(yuanwenIncident);
        System.out.println(yuanwenIncidents);
        if(yuanwenIncidents.size()>0){
            Integer likes = yuanwenIncidents.get(0).getLikes();
            if("成功".equals(userEvent.getOperationstatus())){
                yuanwenIncident.setLikes(++likes);
            }else {
                yuanwenIncident.setLikes(--likes);
            }
            yuanwenIncident.setObjectid(yuanwenIncidents.get(0).getObjectid());
            j=yuanwenIncidentDao.update(yuanwenIncident);
        }else {
            yuanwenIncident.setObjectid(RandomUtils.createIdOX("YI"));
            yuanwenIncident.setLikes(1);
            yuanwenIncident.setCollections(0);
            yuanwenIncident.setObservations(0);
            j=yuanwenIncidentDao.insert(yuanwenIncident);
        }
        return i==j&&j>0;
    }


    /**
     * 是否点赞文章
     * @param object
     * @param userId
     * @return
     */
    public boolean isLikeYuanWen(String object,String userId) {
        UserEvents userEvent = new UserEvents();
        userEvent.setUserid(userId);
        userEvent.setTargetid(object);
        userEvent.setEventtype("点赞");
        userEvent.setOperationstatus("成功");
        List<UserEvents> userEvents = userEventsDao.queryAll(userEvent);
        return userEvents.size()>0;
    }


    /**
     * 是否收藏文章
     * @param object
     * @param userId
     * @return
     */
    public boolean isCollectYuanWen(String object,String userId) {
        UserEvents userEvent = new UserEvents();
        userEvent.setUserid(userId);
        userEvent.setTargetid(object);
        userEvent.setEventtype("收藏");
        userEvent.setOperationstatus("成功");
        List<UserEvents> userEvents = userEventsDao.queryAll(userEvent);
        return userEvents.size()>0;
    }
}
